const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getSetting() {
  return new Promise((res, rej) => {
    wx.getSetting({
      success: res,
      fail: rej
    });
  })
}
function openSetting() {
  return new Promise((res, rej) => {
    wx.openSetting({
      success: res,
      fail: rej
    });
  })
}
function chooseAddress() {
  return new Promise((res, rej) => {
    wx.chooseAddress({
      success: res,
      fail: rej
    });
  })
}
function showModal(content) {
  return new Promise((res, rej) => {
    wx.showModal({
      title: '提示',
      content,
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#037efc',
      success: res,
      fail: rej
    });
      
  })
}
function showToast(title) {
  return new Promise((res, rej) => {
    wx.showToast({
      title,
      icon: 'none',
      success: res,
      fail: rej
    });
      
  })
}
function chooseImage() {
  return new Promise((res, rej) => {
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res,
      fail: rej
    }); 
  })
}
function previewImage(options) {
  return new Promise((res, rej) => {
    wx.previewImage({
      ...options,
      success: res,
      fail: rej
    });
  })
}
function uploadFile(options) {
  return new Promise((res, rej) => {
    wx.uploadFile({
      url: 'https://api.uomg.com/api/image.baidu',
      ...options,
      name: 'file',
      formData: {},
      success: res,
      fail: rej
    });
      
  })
}
function debounce(fun, time=1000) {
  let timer;
  return (...args) => {
    timer && clearTimeout(timer);

    timer = setTimeout(() => {
      fun.apply(this, args)
    }, time)
  }
}
module.exports = {
  formatTime,
  getSetting,
  openSetting,
  chooseAddress,
  showModal,
  showToast,
  chooseImage,
  previewImage,
  debounce
}
